package com.rc.saas.mini.service.rc;

import com.rc.saas.mini.common.base.BaseMapper;
import com.rc.saas.mini.mapper.rc.RcOperatorAccountRecordMapper;
import com.rc.saas.mini.model.member.MemberAccount;
import com.rc.saas.mini.model.member.MemberAccountRecord;
import com.rc.saas.mini.model.member.MemberInfo;
import com.rc.saas.mini.model.rc.RcOperatorAccountRecord;
import com.rc.saas.mini.model.recharge.RechargeOrder;
import com.rc.saas.mini.service.member.MemberInfoService;
import com.rc.saas.mini.service.recharge.RechargeOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class RcOperatorAccountRecordServiceImpl implements RcOperatorAccountRecordService {
    @Autowired
    private RcOperatorAccountRecordMapper rcOperatorAccountRecordMapper;

    @Autowired
    private RechargeOrderService rechargeOrderService;
    @Autowired
    private MemberInfoService memberInfoService;

    @Override
    public RcOperatorAccountRecordMapper getMapper() {
        return rcOperatorAccountRecordMapper;
    }

    @Override
    public void saveByMemberAcountRecord(MemberAccountRecord memberAccountRecord) {
        RcOperatorAccountRecord rcOperatorAccountRecord = new RcOperatorAccountRecord();

        MemberInfo memberInfo = memberInfoService.selectByPrimaryKey(memberAccountRecord.getMemberId());
        //1.充值
        rcOperatorAccountRecord.setTenantCode(memberAccountRecord.getTenantCode());
        rcOperatorAccountRecord.setRecordType("0");
        rcOperatorAccountRecord.setExplains("充值");
        //微信充值
        rcOperatorAccountRecord.setRecordSource("1");
        RechargeOrder rechargeOrder = null;
        if (memberAccountRecord.getLocalTradeNo() != null) {
            rechargeOrder = rechargeOrderService.selectRechargeOrderByOutTradeNo(memberAccountRecord.getLocalTradeNo());
        }
        if (rechargeOrder != null && "0".equals(memberAccountRecord.getAccountType())) {
            //真实用户充值
            if ("1".equals(rechargeOrder.getPaymentType())) {
                rcOperatorAccountRecord.setRecordSource("2");
            }

            //付款方
            rcOperatorAccountRecord.setRecordSource("0");
            rcOperatorAccountRecord.setOutId(memberAccountRecord.getMemberId());
            rcOperatorAccountRecord.setOutName(memberInfo.getNickname());
            rcOperatorAccountRecord.setOutType("1");

            //收款人
            rcOperatorAccountRecord.setEntryAccount(memberAccountRecord.getResult());
            rcOperatorAccountRecord.setEntryId(memberAccountRecord.getMemberId());
            rcOperatorAccountRecord.setEntryName(memberInfo.getNickname());
            rcOperatorAccountRecord.setEntryType("1");
            rcOperatorAccountRecord.setRemark("用户充值");
        } else {
            //活动赠送金额、权益金类型，由平台给用户 付款方
            rcOperatorAccountRecord.setRecordSource("0");
            rcOperatorAccountRecord.setOutId(1L);
            rcOperatorAccountRecord.setOutName("瑞晨公司");
            rcOperatorAccountRecord.setOutType("0");

            //收款人
            rcOperatorAccountRecord.setEntryId(memberAccountRecord.getMemberId());
            rcOperatorAccountRecord.setEntryAccount(memberAccountRecord.getResult());
            rcOperatorAccountRecord.setEntryType("1");
            rcOperatorAccountRecord.setEntryName(memberInfo.getNickname());

            //3.收益金收入
            if("3".equals(memberAccountRecord.getAccountType())){
                rcOperatorAccountRecord.setRemark("收益金赠送");
            }
            //2.活动收入
            if("0".equals(memberAccountRecord.getAccountType())){
                rcOperatorAccountRecord.setRemark("平台活动赠送");
            }
        }

        rcOperatorAccountRecord.setStatus("0");
        rcOperatorAccountRecord.setRecordTime(new Date());
        rcOperatorAccountRecordMapper.insertSelective(rcOperatorAccountRecord);
    }


}
